package com.itheima.stock.face.impl;

import com.itheima.stock.constant.StockConstant;
import com.itheima.stock.face.StockCacheFace;
import com.itheima.stock.mapper.StockBusinessMapper;
import com.itheima.stock.pojo.entity.StockBusiness;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@CacheConfig(cacheNames = StockConstant.STOCK)
public class StockCacheFaceImpl implements StockCacheFace {


    @Autowired
    private StockBusinessMapper stockBusinessMapper;
    /**
     * 获取所有股票编码，并添加上证或者深证的股票前缀编号：sh sz
     *
     * @return
     */

    @Cacheable(key = "#root.method.getName()")
    @Override
    public List<String> getAllStockCodeWithPredix() {
        List<String> codes = stockBusinessMapper.getStockCodes();
        return codes.stream().map(code -> code.startsWith("6") ? "sh" + code : "sz" + code).collect(Collectors.toList());
    }

    /**
     * 根据id更新股票的信息
     *
     * @param info
     */
    @CacheEvict(key = "#root.method.getName()")
    @Override
    public void updateStockInfoById(StockBusiness info) {
        stockBusinessMapper.updateByPrimaryKey(info);
    }
}
